﻿using CRM.Api.Common;
using CRM.Api.Models;
using CRM.Infrastructure.Dto;
using CRM.Infrastructure.Entity;
using CRM.Infrastructure.Models;
using CRM.Infrastructure.Service;
using CRM.Utility;
using Dapper.Extensions;
using System;
using System.Collections.Generic;
using System.Web.Http;

namespace CRM.Api.Controllers
{
    /// <summary>
    /// 线索
    /// </summary>
    public class ClueController : BaseApiController
    {
        CluesService cService = new CluesService();
        CallLogService clService = new CallLogService();

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<Page<CluesDto>> GetPages(ClueSearchModel model)
        {
            //隐藏电话
            var data = cService.GetPages(model);
            foreach (var item in data.rows)
            {
                if (string.IsNullOrWhiteSpace(item.UserName))
                {
                    string first = "";
                    if (!string.IsNullOrWhiteSpace(item.HisUserName))
                    {
                        first = item.HisUserName.Substring(0, 1);
                    }
                    switch (item.Sex)
                    {
                        case 1:
                            item.UserName = first + " 先生";
                            break;
                        case 2:
                            item.UserName = first + " 女士";
                            break;
                        default:
                            item.UserName = first + "未知";
                            break;
                    }
                }
                item.HisMobile = "";
                item.HisUserName = "";
            }
            return ResultData<Page<CluesDto>>.Success(data);
        }

        /// <summary>
        /// 添加外呼记录
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<int> AddCallLog(CallLog log)
        {
            var id = clService.Insert(log);
            return ResultData<int>.Success(EConvert.ToInt(id));
        }

        /// <summary>
        /// 获取统计
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<StatisticModel> GetStatistic(long userId)
        {
            List<int> lst = cService.GetStatistic(userId, DateTime.Now);
            return ResultData<StatisticModel>.Success(new StatisticModel()
            {
                Total = lst[0],
                UnRead = lst[1]
            });
        }

        /// <summary>
        /// 更新线索
        /// </summary>
        /// <param name="clue"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<Clues> UpdateClue(Clues clue)
        {
            var dbClue = cService.SingleOrDefault(clue.ClueID);
            if (!string.IsNullOrEmpty(clue.UserName))
            {
                dbClue.UserName = clue.UserName;
            }
            if (!string.IsNullOrEmpty(clue.EngineNo))
            {
                dbClue.EngineNo = clue.EngineNo;
            }
            if (clue.BrandID > 0)
            {
                dbClue.BrandID = clue.BrandID;
                dbClue.Brand = clue.Brand;
            }
            if (clue.ModelID > 0)
            {
                dbClue.ModelID = clue.ModelID;
                dbClue.Model = clue.Model;
            }
            if (clue.RegisterDate.Year > 1900)
            {
                dbClue.RegisterDate = clue.RegisterDate;
            }
            if (clue.EndDate.Year > 1900)
            {
                dbClue.EndDate = clue.EndDate;
            }
            dbClue.ClueType = clue.ClueType;
            dbClue.UpdateDate = DateTime.Now;
            dbClue.UserName = clue.UserName;
            dbClue.Mobile = clue.Mobile;
            cService.Update(dbClue);
            dbClue.HisMobile = "";
            return ResultData<Clues>.Success(dbClue);
        }

        /// <summary>
        /// 获取线索详情
        /// </summary>
        /// <param name="clueId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<Clues> GetClue(long clueId)
        {
            var dbClue = cService.SingleOrDefault(clueId);
            dbClue.HisMobile = "";
            dbClue.HisUserName = "";
            return ResultData<Clues>.Success(dbClue);
        }

        /// <summary>
        /// 获取同步
        /// </summary>
        /// <param name="companyID">公司ID</param>
        /// <param name="pageSize">最大500</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<CluesDto>> GetSync(int companyID, int pageSize)
        {
            if (pageSize > 500)
            {
                pageSize = 500;
            }
            if (pageSize < 0)
            {
                return ResultData<List<CluesDto>>.Error("参数pageSize参数错误");
            }
            if (companyID < 1)
            {
                return ResultData<List<CluesDto>>.Error("公司companyID错误");
            }
            //隐藏电话
            var data = cService.GetSync(companyID, pageSize);
            foreach (var item in data)
            {
                if (string.IsNullOrWhiteSpace(item.UserName))
                {
                    string first = "";
                    if (!string.IsNullOrWhiteSpace(item.HisUserName))
                    {
                        first = item.HisUserName.Substring(0, 1);
                    }
                    switch (item.Sex)
                    {
                        case 1:
                            item.UserName = first + " 先生";
                            break;
                        case 2:
                            item.UserName = first + " 女士";
                            break;
                        default:
                            item.UserName = first + "未知";
                            break;
                    }
                }
                item.HisMobile = "";
                item.HisUserName = "";
            }

            if (data.Count > 0)
            {
                cService.UpdateSync(data);
            }
            return ResultData<List<CluesDto>>.Success(data);
        }
    }
}
